/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.mule.transport.ftpext;

import org.mule.api.endpoint.MalformedEndpointException;
import org.mule.util.StringUtils;

import java.net.URI;
import java.util.Properties;
import org.mule.endpoint.AbstractEndpointURIBuilder;

/**
 * <code>UrlEndpointBuilder</code> is the default endpointUri strategy suitable for
 * most connectors
 */
public class FtpExtUrlEndpointURIBuilder extends AbstractEndpointURIBuilder {

    @Override
    protected void setEndpoint(URI uri, Properties props) throws MalformedEndpointException {
        address = getAddress(uri);
    }

    public static String getAddress(URI uri) {
        String address = "";
        if (uri.getHost() != null) {
            // set the endpointUri to be a proper url if host and port are set
//            this.address = uri.getScheme() + "://" + uri.getHost();
            address = "ftp" + "://" + uri.getHost();
            if (uri.getPort() != -1) {
                address += ":" + uri.getPort();
            }
        }
        if (StringUtils.isNotBlank(uri.getRawPath())) {
            address += uri.getRawPath();
        }

        if (StringUtils.isNotBlank(uri.getRawQuery())) {
            address += "?" + uri.getRawQuery();
        }

        return address;
    }

//    public static URI dupURI(URI uri, String newPath) {
//        String address = "";
//        URI ret=null;
//        if (uri.getHost() != null) {
//            // set the endpointUri to be a proper url if host and port are set
////            this.address = uri.getScheme() + "://" + uri.getHost();
//            address = "ftp" + "://";
//             String userInfo=uri.getUserInfo();
//             if(StringUtils.isNotBlank(userInfo)){
//                 address+=userInfo+"@";
//             }
//            address+= uri.getHost();
//            if (uri.getPort() != -1) {
//                address += ":" + uri.getPort();
//            }
//        }
//
//        address += newPath;
//        ret= URI.create(address);
//        return ret;
//    }
}
